package com.example.androidtest;

import java.util.ArrayList;

import com.example.fragment.Home_Fragment;
import com.example.fragment.Person_Fragment;
import com.example.fragment.Question_Fragment;
import com.example.fragment.Thing_Fragment;
import com.example.fragment.Wendang_Fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.Toast;

public class Tabhost extends FragmentActivity{
	private TabHost mytabhost ;
	private FragmentManager fm ;
	private ArrayList<View> data;
	private ViewPager viewpager ;
	private MyAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题
		setContentView(R.layout.activity_tabhost);
		
		Toast.makeText(Tabhost.this, "登录成功", Toast.LENGTH_SHORT).show();
		
		int tabLayout[] = {R.layout.tab1,R.layout.tab2,R.layout.tab4,R.layout.tab3,R.layout.tab5};
		int tabContent[] = {R.id.tab1,R.id.tab2,R.id.tab3,R.id.tab4,R.id.tab5};
		mytabhost = (TabHost) findViewById(android.R.id.tabhost);
		mytabhost.setup();
		
		//创建标签，配置图片 ，绑定tab
		for(int i = 0;i<tabLayout.length;i++){
			View view = getLayoutInflater().inflate(tabLayout[i], null);
			mytabhost.addTab(mytabhost.newTabSpec("").setIndicator(view).setContent(tabContent[i]));
		}
		fm = getSupportFragmentManager();//初始化管类器
		FragmentTransaction transaction = fm.beginTransaction();//初始化业务类
		transaction.replace(R.id.tab1, new Home_Fragment());//替换 home_fragment
		transaction.replace(R.id.tab2, new Wendang_Fragment());//替换 Wendang_Fragment
		transaction.replace(R.id.tab3, new Question_Fragment());//替换 Thing_Fragment
		transaction.replace(R.id.tab4, new Thing_Fragment());//替换 Question_Fragment
		transaction.replace(R.id.tab5, new Person_Fragment());//替换 Person_Fragment
		transaction.commit();//提交
//		viewpager = (ViewPager) findViewById(R.id.viewpager);
//		data = new ArrayList<>();
//		View view1 = getLayoutInflater().inflate(R.layout.fragment_home, null);
//		View view2 = getLayoutInflater().inflate(R.layout.fragment_wendang, null);
//		View view3 = getLayoutInflater().inflate(R.layout.fragment_things, null);
//		View view4 = getLayoutInflater().inflate(R.layout.fragment_question, null);
//		View view5 = getLayoutInflater().inflate(R.layout.fragment_person, null);
//		data.add(view1);
//		data.add(view2);
//		data.add(view3);
//		data.add(view4);
//		data.add(view5);
//		
//		adapter = new MyAdapter(data);
//		viewpager.setAdapter(adapter);
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.i("hahahahahaha", "444444444444444444444444444444 ");
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			Log.i("hahahahahaha", "111111111111111111111111111111 ");
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			Log.i("hahahahahaha", "22222222222222222222222222222222 ");
			Log.i("hahahahahaha", "33333333333 ");

			builder.setTitle("系统提示").setMessage("确定要退出吗？").setNegativeButton("确定", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Intent.ACTION_MAIN);
					intent.addCategory(Intent.CATEGORY_HOME);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
					startActivity(intent);
					android.os.Process.killProcess(android.os.Process.myPid());
//					finish();
					
				}
			}).setPositiveButton("取消", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

				}
			}).show();
//			Log.i("hahahahahaha", "333333333333333333333333333333 ");

			break;

		default:
			break;
		}
		return false;
	}

}
